#import "TicTacToeGame.h"

const NSInteger kSizeOfField = 100;
const NSInteger kNumOfRowsTTT = 3;
const NSInteger kNumOfColsTTT = 3;

@interface TicTacToeGame ()

@property NSMutableArray* symbols;
@property BOOL isGameOver;
@property NSInteger remTime;
@property NSTimer* timer;


@end

@implementation TicTacToeGame

-(instancetype) init {
    self = [super init];
    if(self) {
        self.name = @"TicTacToe Game";
        self.type = observation;
        self.symbols = [[NSMutableArray alloc] initWithObjects:@"",@"",@"",@"",@"",@"",@"",@"",@"", nil];
        self.ruleOfTheGame = @"Are there 3 in a line!";
        self.isGameOver = NO;
    }
    return self;
}

-(void) draw {
    NSInteger x = kScreenWidth / 2 - 3*kSizeOfField/2;
    NSInteger y = (kScreenWidth - (2*kButtonHeigh))/2 ;
    for (int i = 0; i < 9; ++i) {
        LabelGameComponent* l = [[LabelGameComponent alloc] initWithText:@"" andFrame:CGRectMake(x, y, kSizeOfField, kSizeOfField) andFontSize:kSizeOfField - 5 andColour:@"redColor"];
        [self addComponents:l];
        x += kSizeOfField;
        if(i % 3 == 2) {
            x = kScreenWidth / 2 - 3*kSizeOfField/2;
            y += kSizeOfField;
        }
    }
    
    
}

-(void) getNewSymbol {
    NSInteger i = arc4random_uniform(9);
    self.remTime = 1;
    self.remTime --;
    if(self.remTime == 0) {
        NSInteger c = arc4random_uniform(2);
        if(c == 0) {
            self.symbols[i] = @"X";
            ((LabelGameComponent*)self.components[i]).text = @"X";
        }
        else {
            self.symbols[i] = @"O";
            ((LabelGameComponent*)self.components[i]).text = @"O";
        }
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"TTTupdateView" object:self];
}

-(void) play {
    if(!self.isGameOver) {
        [self draw];
        NSInteger counter = 0;
        self.timer = [NSTimer scheduledTimerWithTimeInterval: 0.8  target: self selector:@selector(getNewSymbol)userInfo: nil repeats:YES];
        counter ++;

    }
    else {
        [self.timer invalidate];
    }
}

-(BOOL) isRight {
    return ([self.symbols[0] isEqualToString:self.symbols[1]] && [self.symbols[0] isEqualToString:self.symbols[2]] && ![self.symbols[0] isEqual:@""]) ||
            ([self.symbols[3] isEqualToString:self.symbols[4]] && [self.symbols[3] isEqualToString:self.symbols[5]] && ![self.symbols[3] isEqual:@""]) ||
            ([self.symbols[6] isEqualToString:self.symbols[7]] && [self.symbols[6] isEqualToString:self.symbols[8]] && ![self.symbols[6] isEqual:@""]) ||
            ([self.symbols[0] isEqualToString:self.symbols[3]] && [self.symbols[0] isEqualToString:self.symbols[6]] && ![self.symbols[0] isEqual:@""]) ||
            ([self.symbols[1] isEqualToString:self.symbols[4]] && [self.symbols[1] isEqualToString:self.symbols[7]] && ![self.symbols[1] isEqual:@""]) ||
            ([self.symbols[2] isEqualToString:self.symbols[5]] && [self.symbols[2] isEqualToString:self.symbols[8]] && ![self.symbols[2] isEqual:@""]) ||
            ([self.symbols[0] isEqualToString:self.symbols[4]] && [self.symbols[0] isEqualToString:self.symbols[8]] && ![self.symbols[0] isEqual:@""]) ||
    ([self.symbols[2] isEqualToString:self.symbols[4]] && [self.symbols[2] isEqualToString:self.symbols[6]] && ![self.symbols[2] isEqual:@""]);
}

-(void) gameOver {
    self.isGameOver = YES;
    [self.timer invalidate];
    if([self.symbols[0] isEqualToString:self.symbols[1]] && [self.symbols[0] isEqualToString:self.symbols[2]] && ![self.symbols[0] isEqual:@""]) {
        ((LabelGameComponent*)self.components[0]).colour = @"greenColor";
        ((LabelGameComponent*)self.components[1]).colour = @"greenColor";
        ((LabelGameComponent*)self.components[2]).colour = @"greenColor";
    }
    if([self.symbols[3] isEqualToString:self.symbols[4]] && [self.symbols[3] isEqualToString:self.symbols[5]] && ![self.symbols[3] isEqual:@""]) {
        ((LabelGameComponent*)self.components[3]).colour = @"greenColor";
        ((LabelGameComponent*)self.components[4]).colour = @"greenColor";
        ((LabelGameComponent*)self.components[5]).colour = @"greenColor";
    }
    if([self.symbols[6] isEqualToString:self.symbols[7]] && [self.symbols[6] isEqualToString:self.symbols[8]] && ![self.symbols[6] isEqual:@""]) {
        ((LabelGameComponent*)self.components[6]).colour = @"greenColor";
        ((LabelGameComponent*)self.components[7]).colour = @"greenColor";
        ((LabelGameComponent*)self.components[8]).colour = @"greenColor";
    }
    if([self.symbols[0] isEqualToString:self.symbols[3]] && [self.symbols[0] isEqualToString:self.symbols[6]] && ![self.symbols[0] isEqual:@""]) {
        ((LabelGameComponent*)self.components[0]).colour = @"greenColor";
        ((LabelGameComponent*)self.components[3]).colour = @"greenColor";
        ((LabelGameComponent*)self.components[6]).colour = @"greenColor";
    }
    if([self.symbols[1] isEqualToString:self.symbols[4]] && [self.symbols[1] isEqualToString:self.symbols[7]] && ![self.symbols[1] isEqual:@""]) {
        ((LabelGameComponent*)self.components[1]).colour = @"greenColor";
        ((LabelGameComponent*)self.components[4]).colour = @"greenColor";
        ((LabelGameComponent*)self.components[7]).colour = @"greenColor";
    }
    if([self.symbols[2] isEqualToString:self.symbols[5]] && [self.symbols[2] isEqualToString:self.symbols[8]] && ![self.symbols[2] isEqual:@""]) {
        ((LabelGameComponent*)self.components[2]).colour = @"greenColor";
        ((LabelGameComponent*)self.components[5]).colour = @"greenColor";
        ((LabelGameComponent*)self.components[8]).colour = @"greenColor";
    }
    if([self.symbols[0] isEqualToString:self.symbols[4]] && [self.symbols[0] isEqualToString:self.symbols[8]] && ![self.symbols[0] isEqual:@""]) {
        ((LabelGameComponent*)self.components[0]).colour = @"greenColor";
        ((LabelGameComponent*)self.components[4]).colour = @"greenColor";
        ((LabelGameComponent*)self.components[8]).colour = @"greenColor";
    }
    if([self.symbols[2] isEqualToString:self.symbols[4]] && [self.symbols[2] isEqualToString:self.symbols[6]] && ![self.symbols[2] isEqual:@""]) {
        ((LabelGameComponent*)self.components[2]).colour = @"greenColor";
        ((LabelGameComponent*)self.components[4]).colour = @"greenColor";
        ((LabelGameComponent*)self.components[6]).colour = @"greenColor";
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"TTTupdateView" object:self];
}


-(NSString*) symbolForIndex : (NSInteger) i {
    return (NSString*)self.symbols[i];
    
}

@end
